<!DOCTYPE html>
<html>
<head>
    <#include "/pay/include/head_view.html" />
</head>
<body>
<div id="app">
    <i-form ref="myForm" :model="myform" :rules="ruleValidate" :label-width="120">
        <Form-item label="商品名称" prop="prodName">
            <i-input v-model="myform.prodName"  placeholder="请输入"></i-input>
        </Form-item>
        <Form-item label="商品价格" prop="prodPrice">
            <i-input v-model="myform.prodPrice" placeholder="请输入"></i-input>
        </Form-item>
        <Form-item label="是否需要配送" prop="addressType">
            <i-select v-model="myform.addressType" :value="myform.addressType">
                <i-option value=0>不需要</i-option>
                <i-option value="1">需要</i-option>
            </i-select>
        </Form-item>

        <Form-item label="状态" prop="state">
            <i-switch v-model="myform.state" size="large" :true-value="1" :false-value="0">
                <span slot="open">有效</span>
                <span slot="close">无效</span>
            </i-switch>
        </Form-item>
    </i-form>
    <div>
        <i-button type="info" icon="shuffle" @click="save"  long> 保 存  </i-button>
    </div>
</div>
<script>


    const urls = {
        query:'/api/pay/payprod/query'
        ,save:'/api/pay/payprod/save'
    };

    var vm = new Vue({
        el: '#app',
        data: {
                myform: {
                    isState:true,
                    state: 1,
                    payType:'1',
                    prodPrice:0,
                    id:null//主键
                },
                ruleValidate: {
                    prodName: [
                        { required: true, message: '请输入商品名称', trigger: 'blur' },
                    ],
                    prodPrice: [
                        { required: true, message: '请输入商品价格', trigger: 'blur' },
                        { type: 'string', message: '商品价格格式不对', trigger: 'blur',pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/ }
                    ]
                }
        },
        created:function(){
            if (this.getPara('id')) {
                this.myform.id = this.getPara('id');
                this.loadData();
            }
        },
        methods: {
            loadData:function(){
                $.ajax(
                    {
                        type: 'post', // 提交方式 get/post
                        url:urls.query, // 需要提交的 u
                        async:true,
                        cache:false,
                        dataType:'json',
                        data:this.myform,
                        success: function(ret){
                            if(ret.success){
                                vm.myform = ret.data;
                                if(vm.myform.addressType!=null){
                                    vm.myform.addressType = vm.myform.addressType+"";
                                }
                                if(vm.myform.prodPrice!=null){
                                    vm.myform.prodPrice = vm.myform.prodPrice+"";
                                }
                            }
                        },
                        error:function(){
                            //layer.alert("系统错误，请稍候再试");
                        }
                    });
            },
            getPara:function(name) {
                var r = new RegExp("(\\?|#|&)" + name + "=([^&#]*)(&|#|$)");
                var m = location.href.match(r);
                return (!m ? "" : m[2]);
            },
            save:function() {
                this.$refs['myForm'].validate(function(valid) {
                    if (valid) {
                        $.ajax(
                            {
                                type: 'post', // 提交方式 get/post
                                url:urls.save, // 需要提交的 u
                                async:true,
                                cache:false,
                                dataType:'json',
                                data:vm.myform,
                                success: function(ret){
                                    if(ret.success){
                                        parent.layer.msg('保存成功');
                                        var i = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(i);
                                        //parent.layer.
                                    }
                                },
                                error:function(){
                                    //layer.alert("系统错误，请稍候再试");
                                }
                            });
                    }
                })
            }
        }
    });


    function save() {
        vm.save();
    }

</script>

<#include "/pay/include/hmt.html" />
</body>
</html>
